package cn.ac.iscas.dao.mapper.dameng;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.StringReader;
import java.sql.*;

/**
 * @author zhuquanwen
 * @version 1.0
 * @date 2023/12/11 18:29
 */
@MappedTypes({String.class})
@MappedJdbcTypes(value = {JdbcType.CLOB})
public class ClobTypeHandler extends BaseTypeHandler<String> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        StringReader reader = new StringReader(parameter);
        ps.setCharacterStream(i, reader, parameter.length());
    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String value = "";
        Clob clob = rs.getClob(columnName);
        if(clob != null) {
            int size = (int)clob.length();
            value = clob.getSubString(1L, size);
        }

        return value;
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String value = "";
        Clob clob = rs.getClob(columnIndex);
        if(clob != null) {
            int size = (int)clob.length();
            value = clob.getSubString(1L, size);
        }
        return value;
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String value = "";
        Clob clob = cs.getClob(columnIndex);
        if(clob != null) {
            int size = (int)clob.length();
            value = clob.getSubString(1L, size);
        }
        return value;
    }
}
